package com.sloan.community.config;

import com.sloan.community.po.BasePO;
import com.sloan.community.po.User;
import com.sloan.community.util.SnowflakeIdGenerator;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.mapping.OutboundRow;
import org.springframework.data.r2dbc.mapping.event.BeforeSaveCallback;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.Objects;
//  TODO BeforeSaveCallback 标准的字段中 createdBy,createTime,updateBy,updateTime 并不会生效,很奇怪
//@Component
@Deprecated
public class DefaultBeforeSaveCallBack implements BeforeSaveCallback<BasePO> {

    @Autowired
    private SnowflakeIdGenerator generator;

    @Override
    public Publisher<BasePO> onBeforeSave(BasePO entity, OutboundRow row, SqlIdentifier table) {
        if(Objects.isNull(entity.getId())){
            entity.setId(generator.nextId());
            entity.setCreatedBy(0L);
            entity.setCreatedTime(LocalDateTime.now());
        }else {
            entity.setUpdatedBy(entity.getId());
            entity.setUpdatedTime(LocalDateTime.now());
        }
        return Mono.just(entity);
    }
}
